package com.example.administrator.zhongyichou.base;

import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Build;
import android.os.Handler;
import android.os.StrictMode;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.administrator.zhongyichou.config.Constants;
import com.example.administrator.zhongyichou.R;
import com.lzy.ninegrid.NineGridView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreater;
import com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreater;
import com.scwang.smartrefresh.layout.api.RefreshFooter;
import com.scwang.smartrefresh.layout.api.RefreshHeader;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;

/**
 * Created by lichenyu on 2017/8/9.
 * 全局单例
 */

public class MyApplication extends Application {

    /**
     * 上下文
     */
    private static Context mContext;

    /**
     * 主线程的 Handler
     */
    private static Handler mMainThreadHandler;

    /**
     * 主线程的线程 id
     */
    private static int mMainThreadId;

    private static MyApplication mApplication;

    private static SharedPreferences sharedPreferences;
    private static SharedPreferences.Editor editor;

    @Override
    public void onCreate() {
        super.onCreate();
        //上下文
        mContext = getApplicationContext();

        mApplication = this;

        //主线程的 handler
        mMainThreadHandler = new Handler();

        // 主线程的线程id
        mMainThreadId = android.os.Process.myTid();

        initSP();
        //初始化九宫格控件
        NineGridView.setImageLoader(new GlideImageLoader());

        initCarema();
    }

    //    防止Android7.0相机调用崩溃
    private void initCarema() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
            StrictMode.setVmPolicy(builder.build());
        }
    }

    /**
     * Glide 加载
     */
    private class GlideImageLoader implements NineGridView.ImageLoader {
        @Override
        public void onDisplayImage(Context context, ImageView imageView, String url) {
            Glide.with(context).load(url)//
                    .placeholder(R.color.black1)  //设置占位图
                    .error(R.color.black1)
                    .diskCacheStrategy(DiskCacheStrategy.ALL)//
                    .into(imageView);
        }

        @Override
        public Bitmap getCacheImage(String url) {
            return null;
        }
    }

    /**
     * 得到上下文
     */
    public static Context getContext() {
        return mContext;
    }

    /**
     * 得到主线程里面创建的一个 handler
     */
    public static Handler getMainThreadHandler() {
        return mMainThreadHandler;
    }

    /**
     * 得到主线程的线程 id
     */
    public static int getMainThreadId() {
        return mMainThreadId;
    }


    public static synchronized MyApplication getInstance() {
        return mApplication;
    }

    static {
        SmartRefreshLayout.setDefaultRefreshHeaderCreater(new DefaultRefreshHeaderCreater() {
            @Override
            public RefreshHeader createRefreshHeader(Context context, RefreshLayout layout) {
                layout.setPrimaryColorsId(R.color.black1, R.color.black2);//全局设置主题颜色
                return new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.Translate);//指定为经典Header，默认是 贝塞尔雷达Header
            }
        });
        //设置全局的Footer构建器
        SmartRefreshLayout.setDefaultRefreshFooterCreater(new DefaultRefreshFooterCreater() {
            @Override
            public RefreshFooter createRefreshFooter(Context context, RefreshLayout layout) {
                //指定为经典Footer，默认是 BallPulseFooter
                return new ClassicsFooter(context).setSpinnerStyle(SpinnerStyle.Translate);
            }
        });
    }


    private void initSP() {
        if (sharedPreferences == null) {
            sharedPreferences = getSharedPreferences("gongyichou_user", MODE_PRIVATE);
        }
        editor = sharedPreferences.edit();
    }

    public static void putSP(String key, String value) {
        editor.putString(key, value);
        editor.commit();
    }

    public static void putSP(String key, boolean value) {
        editor.putBoolean(key, value);
        editor.commit();
    }

    public static void putSP(String key, int value) {
        editor.putInt(key, value);
        editor.commit();
    }

    public static String getSPValue(String key, String defaultValue) {
        return sharedPreferences.getString(key, defaultValue);
    }

    public static boolean getSPValue(String key, boolean defaultValue) {
        return sharedPreferences.getBoolean(key, defaultValue);
    }

    public static int getSPValue(String key, int defaultValue) {
        return sharedPreferences.getInt(key, defaultValue);
    }

    public static String getUserID() {
        return getSPValue(Constants.USER_ID, "");
    }

    public static SharedPreferences getSP() {
        return sharedPreferences;
    }

}
